package com.ujs.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ujs.eduservice.entity.EduChapter;
import com.ujs.eduservice.entity.EduVideo;
import com.ujs.eduservice.entity.course.ChapterVo;
import com.ujs.eduservice.entity.course.VideoVo;
import com.ujs.eduservice.mapper.EduChapterMapper;
import com.ujs.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujs.eduservice.service.EduVideoService;
import com.ujs.servicebase.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.AreaAveragingScaleFilter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-08-07
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideo(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<EduChapter>().eq("course_id", courseId);
        // 课程id对应的所有 chapter
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper);
        if (eduChapters != null && eduChapters.size() > 0) {
            ArrayList<ChapterVo> chapterVos = new ArrayList<>();
            for (EduChapter eduChapter : eduChapters) {
                // 根据chapter查询所有的 video
                ChapterVo chapter = new ChapterVo();
                chapter.setId(eduChapter.getId());
                chapter.setTitle(eduChapter.getTitle());
                QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<EduVideo>().eq("chapter_id", chapter.getId());
                List<EduVideo> videos = videoService.list(videoQueryWrapper);
                if (videos != null && videos.size() > 0) {
                    ArrayList<VideoVo> videoVos = new ArrayList<>();
                    for (EduVideo video : videos) {
                        VideoVo videoVo = new VideoVo();
                        videoVo.setId(video.getId());
                        videoVo.setTitle(video.getTitle());
                        videoVo.setVideoSourceId(video.getVideoSourceId());
                        videoVos.add(videoVo);
                    }
                    chapter.setChildren(videoVos);
                }
                chapterVos.add(chapter);
            }
            return chapterVos;
        } else {
            throw new GuliException(20001, "该课程没有章节信息");
        }
    }

    @Override
    public void deleteChapter(String chapterId) {
        // 查询该章节是否有小节
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<EduVideo>().eq("chapter_id", chapterId);
        int count = videoService.count(videoWrapper);
        if (count > 0) {
            throw new GuliException(20001, "该章节有小节，不能删除");
        }
        baseMapper.deleteById(chapterId);
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        try{
            QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<EduChapter>().eq("course_id", courseId);
            baseMapper.delete(queryWrapper);
        } catch (Exception e) {
            throw new GuliException(20001, "删除章节失败");
        }

    }
}
